<?php
/**
 * tpAdmin [a web admin based ThinkPHP5]
 *
 * @author yuan1994 <tianpian0805@gmail.com>
 * @link http://tpadmin.yuan1994.com/
 * @copyright 2016 yuan1994 all rights reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */

//------------------------
// 招聘列表
//-------------------------

namespace app\admin\controller;

use app\admin\Controller;
use think\Db;
use think\Lang;
use think\Loader;
use think\Exception;

class Info extends Controller
{
    use \app\admin\traits\controller\Controller;

    /**
     * 列表
     */
    public function index()
    {
        $model = $this->getModel('info');

        $listRows = 10;
	
		$field = "u.img,u.name,i.id,i.content,i.photo,i.address,i.count,i.create_time,i.go_time,i.cate_id,i.status,i.isdelete";
        // 分页查询
        $list = array();
        $list = $model->alias("i")
                ->join("tp_user u","i.user_id = u.id","left")
                ->field($field)
                ->where(array("i.isdelete"=>0))
                ->order('  i.id desc')->paginate($listRows, false, ['query' => $this->request->get()]);
        /*foreach($list as $k => $v){
            $list[$k]['create_time'] = date("m-d H:i",$v['create_time']);
            $list[$k]['go_time'] = date("m月d日",$v['go_time']);
        }*/

		foreach($list as $k => $v){
        	$data[$k]['img'] = $v['img'];
        	$data[$k]['name'] = $v['name'];
        	$data[$k]['id'] = $v['id'];
        	$data[$k]['content'] = $v['content'];
        	$data[$k]['photo'] = $v['photo'];
        	$data[$k]['address'] = $v['address'];
        	$data[$k]['count'] = $v['count'];
        	$data[$k]['create_time'] = date('Y-m-d h:i:s',$v['create_time']);
        	$data[$k]['go_time'] = date('Y-m-d h:i:s',$v['go_time']);
        	$data[$k]['cate_id'] = $v['cate_id'];
        	$data[$k]['status'] = $v['status'];
        	$data[$k]['isdelete'] = $v['isdelete'];
        }
        
        // 模板赋值显示
        $this->view->assign('list', $data);
        $this->view->assign("page", $list->render());
        $this->view->assign("count", $list->total());
        $this->view->assign('numPerPage', $listRows);

        return $this->view->fetch();
    }

    /**
     * 编辑
     * @return mixed
     */
    public function edit()
    {
        $controller = $this->request->controller();
        $module = $this->request->module();
        if ($this->request->isAjax()) {
            // 更新
            $data = $this->request->post();
            if (!$data['id']) {
                return ajax_return_adv_error("缺少参数ID");
            }
            
            // 验证
            if (class_exists(Loader::parseClass($module, 'validate', $controller))) {
                $validate = Loader::validate($controller);
                if (!$validate->check($data)) {
                    return ajax_return_adv_error($validate->getError());
                }
            }
            // 更新数据
            Db::startTrans();
            try {
                $data['create_time'] = strtotime($data['create_time']);
                $data['go_time'] = strtotime($data['go_time']);
                Db::table("tp_info")->where(" id = ".$data['id'])->update($data);
                // 提交事务
                Db::commit();

                return ajax_return_adv("编辑成功");
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();

                return ajax_return_adv_error($e->getMessage());
            }
        } else {
            // 编辑
            $id = $rId = $this->request->param('id');
            if (!$id) {
                throw new Exception("缺少参数ID");
            }
            $vo = $this->getModel($controller)->find($rId);
			$vo['create_time'] = date('Y-m-d h:i:s',$vo['create_time']);
			$vo['go_time'] = date('Y-m-d h:i:s',$vo['go_time']);
        	
            if (!$vo) {
                throw new HttpException(404, '该记录不存在');
            }
            $this->view->assign("vo", $vo);

            return $this->view->fetch();
        }
    }
}